package com.tianxiabuyi.weixin.qy.contact;

import com.alibaba.fastjson.annotation.JSONField;
import com.tianxiabuyi.utils.JSONUtil;
import com.tianxiabuyi.weixin.common.WxClient;
import com.tianxiabuyi.weixin.qy.QyWxClientFactory;
import com.tianxiabuyi.weixin.qy.contact.tag.Tag;
import com.tianxiabuyi.weixin.qy.contact.tag.TagUsers;
import com.tianxiabuyi.weixin.qy.contact.tag.TagUsersResult;
import java.util.List;

/**
 * 标签管理
 */
public class Tags {


    private WxClient wxClient;

    /**
     * 初始化wxclient
     * @param corpId
     * @param corpSecret
     * @return
     */
    public static Tags with(String corpId, String corpSecret) {
        Tags tags = new Tags();
        tags.setWxClient(QyWxClientFactory.getInstance().with(corpId, corpSecret));
        return tags;
    }

    /**
     * 设置wxclient，如果已使用with初始化，则不用调用该方法
     * @param wxClient
     */
    public void setWxClient(WxClient wxClient) {
        this.wxClient = wxClient;
    }

    /**
     * 创建标签
     * @param tag
     */
    public void create(Tag tag) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create";
        String json = JSONUtil.toJson(tag);
        wxClient.post(url, json);
    }

    /**
     * 更新标签
     * @param tag
     */
    public void update(Tag tag) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/update";
        String json = JSONUtil.toJson(tag);
        wxClient.post(url, json);
    }

    public void delete(int id) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete?tagid=%s";
        wxClient.get(String.format(url, id));
    }

    /**
     * 获取企业号下所有标签
     * @return
     */
    public List<Tag> list() {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list";

        String response = wxClient.get(url);
        TagList tagList = JSONUtil.fromJson(response, TagList.class);
        return tagList.getTags();
    }

    /**
     * 给标签内添加用户或部门
     * @param tagId
     * @param users
     * @param departments
     * @return
     */
    public TagUsersResult addUsers(int tagId, List<String> users, List<Integer> departments) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers";
        TagUsersWrapper tagUsers = new TagUsersWrapper();
        tagUsers.setTagId(tagId);
        tagUsers.setUsers(users);
        tagUsers.setDepartments(departments);
        String json = JSONUtil.toJson(tagUsers);
        String response = wxClient.post(url, json);
        return JSONUtil.fromJson(response, TagUsersResult.class);
    }

    /**
     * 删除标签内用户或部门
     * @param tagId
     * @param users
     * @param departments
     * @return
     */
    public TagUsersResult deleteUsers(int tagId, List<String> users, List<Integer> departments) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers";
        TagUsersWrapper tagUsers = new TagUsersWrapper();
        tagUsers.setTagId(tagId);
        tagUsers.setUsers(users);
        tagUsers.setDepartments(departments);
        String json = JSONUtil.toJson(tagUsers);
        String response = wxClient.post(url, json);
        return JSONUtil.fromJson(response, TagUsersResult.class);
    }

    /**
     * 获取指定标签下所有用户
     * @param id
     * @return
     */
    public TagUsers getUsers(int id) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?tagid=%s";
        String response = wxClient.get(String.format(url, id));
        TagUsers tagUsers = JSONUtil.fromJson(response, TagUsers.class);
        tagUsers.setTagId(id);
        return tagUsers;
    }

    /**
     * 标签列表
     */
    public static class TagList {

        @JSONField(name = "taglist")
        private List<Tag> tags;

        public List<Tag> getTags() {
            return tags;
        }

        public void setTags(List<Tag> tags) {
            this.tags = tags;
        }
    }

    public static class TagUsersWrapper {

        @JSONField(name = "tagid")
        private int tagId;

        @JSONField(name = "userlist")
        private List<String> users;

        @JSONField(name = "partylist")
        private List<Integer> departments;

        public int getTagId() {
            return tagId;
        }

        public void setTagId(int tagId) {
            this.tagId = tagId;
        }

        public List<String> getUsers() {
            return users;
        }

        public void setUsers(List<String> users) {
            this.users = users;
        }

        public List<Integer> getDepartments() {
            return departments;
        }

        public void setDepartments(List<Integer> departments) {
            this.departments = departments;
        }
    }
}
